package cn.tedu.rtc.mqtt.service.mqtttopic;

import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Topic订阅服务
 * 使用和MqttInboundConfiguration中相同的MqttPahoMessageDrivenChannelAdapter类以订阅
 */
@Component
@Slf4j
public class MqttTopicServiceImpl implements IMqttTopicService {

    @Resource
    private MqttPahoMessageDrivenChannelAdapter adapter;

    @Override
    public void subscribe(String topic) {
        log.debug("subscribe topic: {}", topic);
        adapter.addTopic(topic);
    }

    @Override
    public void subscribe(String topic, int qos) {
        log.debug("subscribe topic: {}", topic);
        adapter.addTopic(topic, qos);
    }

    @Override
    public void unsubscribe(String topic) {
        log.debug("unsubscribe topic: {}", topic);
        adapter.removeTopic(topic);
    }

    public String[] getSubscribedTopic() {
        return adapter.getTopic();
    }
}
